﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace BTKERP.Common.ConfigHelper
{
    public class SystemConfigHelper
    {
        public static bool GetBoolValue(string key)
        {
            string[] strs = ConfigurationManager.AppSettings.GetValues(key);
            if (strs != null && strs.Length == 1)
            {
                if (strs[0].ToLower() == "true" || strs[0].ToLower() == "yes" || strs[0] == "1")
                {
                    return true;
                }
            }
            return false;
        }

        public static int GetIntValue(string key)
        {
            string[] strs = ConfigurationManager.AppSettings.GetValues(key);
            if (strs != null && strs.Length == 1)
            {
                int result = 0;
                if (int.TryParse(strs[0], out result))
                    return result;
            }
            return 0;
        }

        public static string GetStringValue(string key)
        {
            string[] strs = ConfigurationManager.AppSettings.GetValues(key);
            if (strs != null && strs.Length == 1)
            {
                return strs[0];
            }
            return string.Empty;
        }

        public static string GetConnectionString(string key)
        {
            try
            {
                return ConfigurationManager.ConnectionStrings[key].ConnectionString;
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string GetConnectionString(int index)
        {
            if (index >= CheckConnections())
            {
                return string.Empty;
            }
            try
            {
                return ConfigurationManager.ConnectionStrings[index].ConnectionString;
            }
            catch
            {
                return string.Empty;
            }
        }

        public static int CheckConnections()
        {
            return ConfigurationManager.ConnectionStrings.Count;
        }
    }
}
